using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Keep.Const;
using Keep.Model;
using Keep.Util;
using Serilog;
using SqlSugar;

namespace Keep.Service.Impl;

public class MusicOfListServiceImpl : IMusicOfListService
{

    private readonly IMusicListService musicListService;

    public MusicOfListServiceImpl(IMusicListService musicListService)
    {
        this.musicListService = musicListService;
    }

    public bool AddMusicOfList(MusicOfList musicOfList)
    {
        List<MusicOfList> list = SelMusicOfList(new MusicOfList());
        if (list != null && list.Count > 0)
        {
            foreach (MusicOfList ml in list)
            {
                if (ml.name.Equals(musicOfList.name))
                {
                    Log.Error("列表名称已经被使用了" + musicOfList.name);
                    return false;
                }
            }
        }
        musicOfList.status = Static.Enable;
        int i = SqlSugarHelper.Db.Insertable(musicOfList).ExecuteCommand();
        return i > 0;
    }

    public bool DelMusicOfList(int id)
    {
        int i = SqlSugarHelper.Db.Updateable<MusicOfList>()
                .SetColumns(it => it.status == Static.Disable)
                .Where(lt => lt.id == id)
                .ExecuteCommand();
        return i > 0;
    }

    public bool EditMusicOfList(MusicOfList musicOfList)
    {
        int i = SqlSugarHelper.Db.Updateable<MusicOfList>()
               .SetColumnsIF(!String.IsNullOrEmpty(musicOfList.name), it => it.name == musicOfList.name)
               .Where(lt => lt.id == musicOfList.id)
               .ExecuteCommand();
        return i > 0;
    }

    public List<MusicOfList> SelMusicOfList(MusicOfList musicOfList)
    {
        var query = SqlSugarHelper.Db.Queryable<MusicOfList>();
        query.Where(lt => lt.status.Equals(Static.Enable));
        if (!String.IsNullOrEmpty(musicOfList.name))
        {
            query.Where(lt => lt.name.Contains(musicOfList.name));
        }
        query.OrderByDescending(lt => lt.id);
        return query.ToList();
    }


    public List<MusicList> SelMusicList()
    {
        List<MusicList> mlist = musicListService.SelMusicList(new MusicList());

        var result = SqlSugarHelper.Db.Queryable<MusicOfList>()
                        .Where(lt => lt.status.Equals(Static.Enable))
                       .GroupBy(it => it.mlid)
                       .OrderByDescending(lt => lt.id)
                       .Select((MusicOfList it) => new
                       {
                           tid = it.mlid,
                           tsize = SqlFunc.AggregateCount(it.id)
                       })
                       .ToList();
        if (result != null && result.Count > 0)
        {
            foreach (var ml in mlist)
            {
                foreach (var item in result)
                {
                    if (ml.id == item.tid)
                    {
                        ml.num = item.tsize;
                    }
                }
            }
        }
        return mlist;
    }

    public List<MusicOfList> SelMusicListBymlid(int id)
    {
        var query = SqlSugarHelper.Db.Queryable<MusicOfList>();
        query.Where(lt => lt.status.Equals(Static.Enable) && lt.mlid == id);
        query.OrderByDescending(lt => lt.id);
        List<MusicOfList> list = query.ToList();
        if (list != null && list.Count > 0)
        {
            string realpath = Path.Combine(Static.ReqUrl, Static.MusicPath);
            int index = 0;
            foreach (MusicOfList mol in list)
            {
                string path = Path.Combine(realpath, mol.name).Replace("\\", "/");
                string thimg = path.Replace(Static.MusicPath + "/", Static.MusicImgPath + "/");
                mol.path = path;
                mol.thumbnail = Path.ChangeExtension(thimg, "jpg");
                mol.index = index;
                index++;
            }
        }
        return list;
    }

}
